<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>修改密码</title>
    <meta name="keywords" content="基层e生">
    <meta name="description" content="基层e生">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="../../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../../css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="../../css/animate.css" rel="stylesheet">
    <link href="../../css/plugins/ztree/style.css?v=4.1.0" rel="stylesheet">

</head>
<body class="form-horizontal">
 <div class="wrapper wrapper-content animated fadeInRight"> 
   <div class="col-sm-6" style="margin-top: 50px;margin-left: 50px ; " > 
	    <div class="form-group">
	        <label for="userName" class="col-sm-3 control-label">原密码</label>
	        <div class="col-sm-8">
	          <input type="text" class="form-control"  id="oldPwd" placeholder="请输入原密码">
	        </div>
		</div>
		<div class="form-group">
	        <label for="idCard" class="col-sm-3 control-label">请输入新密码</label>
	        <div class="col-sm-8">
	            <input type="text" class="form-control"  id="updatePwd1"  placeholder="输入新密码">
	        </div>
	    </div>
		<div class="form-group">
	        <label for="idCard" class="col-sm-3 control-label">请确认新密码</label>
	        <div class="col-sm-8">
	            <input type="text" class="form-control" id="updatePwd2"  placeholder="输入新密码">
	        </div>
	    </div>
		
	    <div class="form-group">
            <button id="update" class="btn" style="margin-left: 50%;margin-top: 30px"onclick="operate.updateInfo()">提交</button>
        </div>
	
</div>
</div> 
</body>
<!-- 全局js -->
<script src="../../js/jquery.min.js?v=2.1.4"></script>
<script src="../../js/bootstrap.min.js?v=3.3.6"></script>
<script type="text/javascript" src="../../js/jquery.md5.js"></script>
<!-- 自定义js -->
<script src="../../js/content.js?v=1.0.0"></script>
<!--MVVM框架-->
<script type="text/javascript" src="../../js/plugins/knockout/knockout-3.4.2.js"></script>
<script type="text/javascript" src="../../js/plugins/knockout/knockout.mapping-latest.js"></script>
<!-- Bootstrap table -->
<script src="../../js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="../../js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
<script src="../../js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript" src="../../js/common/auth.js"></script>

<script type="text/javascript">
    var __updateUrl = "/appAgent/modifyPasscodeByOld";//post
    var __getCurrent = "/agent/current" ;//获取当前用户
	var __listDeptUrl = "/dept/read/list";
	var maxHeight = top.document.body.clientHeight * 1 ; //文档高度的80%
    var newModel;

	$(function () {
		
		$$ajax({
	        url: __getCurrent,
	        type: "get",
	        contentType: 'application/json',
	        success: function(response) {
	            if (response.code == 200) {
	                console.log("查询成功");
                    response.data.amount =  response.data.amount/100;
                    newModel = ko.mapping.fromJS(response.data);
					ko.applyBindings(newModel,document.body);
	            } else {
                    console.log(data.msg);
                    window.location.href = "login.html";
	            }
	        }
		});
    });

	// var koModel={
    //     updatePwdModel:{
    //         id:ko.observable(),
	// 	    oldPwd:ko.observable(),
    //         updatePwd1:ko.observable(),
    //         updatePwd2:ko.observable()
    //     }

    // };
	var operate = {
        //初始化按钮事件
        operateInit: function () {
            //this.operateAdd();
            //this.operateUpdate();
            //this.operateDelete();
            this.updatePwd();
			this.updateInfo();
        },
	
        //修改用户密码
        updatePwd:function(id){
		
         	alert(newModel),
			 alert("我是ID"+id),
		    newModel.id(id);
            top.layer.open({
                title:"修改用户密码",
                type: 1,
                area: "500px",
                maxHeight:maxHeight,
				content:$get("/html/sys_base_user/updatePwd.html"),
                yes: function(index, layerDom){
                	var close = top.layer.load(1);
                    var jsModle = ko.mapping.toJS(newModel);
                    var pass = "^[A-Za-z0-9]{1,}$";
                    if(!jsModle.updatePwd1||!jsModle.updatePwd2){
                    	top.layer.close(close);
                        top.layer.alert('密码不能为空或空格!', {icon: 6});
                        return;
                    }
                    if(!jsModle.updatePwd1.match(pass)){
                    	top.layer.close(close);
                        top.layer.alert('密码只能是数字与字母组成!', {icon: 6});
                        return;
                    }
                    if(jsModle.updatePwd1 != jsModle.updatePwd2){
                    	top.layer.close(close);
                        top.layer.alert('密码不一致，请输入正确密码!', {icon: 6});
                        return;
                    }
                    var dataUpdate={id:jsModle.id,memPassword:jsModle.updatePwd1};
				    $$ajax({
                        url: __addOrUpdateUrl,
                        type: 'post',//提交的方式
                        dataType:'json',
                        contentType:"application/json; charset=utf-8",
                        data:JSON.stringify(dataUpdate),
                        success: function(data) {
                            if (data.httpCode==200) {
                            	top.layer.close(close);
                                top.layer.alert("更新成功");
                                top.layer.close(index);
                            } else{
                            	top.layer.close(close);
                                alert(data.msg);
                            }
                        }
                    });
                },
                btn2:function(index,layarDom){//关闭
                    ko.cleanNode(layarDom.get(0));
                },
                success:function(layarDom,index){//弹层加载成功
                    ko.applyBindings(newModel, layarDom.get(0));
                    //var unmapped = ko.mapping.toJS(operate.entityModel);
                }
            });
        },

		updateInfo:function(){
			// var aaaa =  newModel.idNum()
			// var jsModle = ko.mapping.toJS(newModel);
			// var dataUpdate={memId:jsModle.id,idNum:jsModle.idNum,agentName:jsModle.agentName};
				    // $$ajax({
                    //     url: __changeDetail,
                    //     type: 'post',//提交的方式
                    //     dataType:'json',
                    //     contentType:"application/json; charset=utf-8",
					// 	//data:dataUpdate,
					// 	 data:JSON.stringify(dataUpdate),
                    //     success: function(data) {
					// 		alert(data.code)
                    //         if (data.code==200) {
                    //         	alert("更新成功")
                    //         } else{
					// 			alert("更新失败")
								
                    //             alert(data.msg);
                    //         }
                    //     }
                    // });
                    alert("我是编号"+newModel.id());
                    alert($.md5("123456"));
                    alert("选密码1择器"+$("#updatePwd1").val());
                    alert("密2222码"+$("#updatePwd2").val());
                    alert(!$("#updatePwd1").val()==$("#updatePwd2").val())
                    var pass = "^[A-Za-z0-9]{1,}$";
                    if($("#updatePwd1").val()==(""||0||undefined||null) || $("#updatePwd2").val()==(""||0||undefined||null)){
                    alert("请输入密码")
                        return;
                    }
                    if(!$("#updatePwd1").val().match(pass)){
                        alert('密码只能是数字与字母组成!');
                        return;
                    }
                    if(!($("#updatePwd1").val() == $("#updatePwd2").val())){
                       alert('密码不一致，请输入正确密码!');
                        return;
                    }
                    if(($("#updatePwd1").val() == $("#updatePwd2").val()==$("#oldPwd").val())){
                       alert('原密码和要修改的密码一致!');
                        return;
                    }
                    var md5OldPwd = $.md5($("#oldPwd").val());
                    var md5NewPwd = $.md5($("#updatePwd1").val());
                    var dataUpdate={memId:newModel.id(),pwd:md5OldPwd,newPwd:md5NewPwd};
				    alert(dataUpdate)
                    $$ajax({
                        url: __updateUrl,
                        type: 'post',//提交的方式
                        dataType:'json',
                        contentType:"application/json; charset=utf-8",
                        data:JSON.stringify(dataUpdate),
                        success: function(data) {
                            if (data.httpCode==200) {
                                top.layer.alert("更新成功");
                             
                            } else{
                                alert(data.msg);
                            }
                        }
                    });
        }
	}
    
</script>

</html>